vim.g.mapleader = ' '
-- vim.g.maplocalleader = ' '
local map_n = function(key, func, desc)
	vim.keymap.set('n', key, func, { desc = desc })
end

-- Clear highlights on search when pressing <Esc> in normal mode
--  See `:help hlsearch`
map_n('<Esc>', '<cmd>nohlsearch<CR>')

-- Diagnostic keymaps
map_n('<leader>q', vim.diagnostic.setloclist, 'Open diagnostic [Q]uickfix list')
--?

-- Exit terminal mode in the builtin terminal with a shortcut that is a bit easier
-- for people to discover. Otherwise, you normally need to press <C-\><C-n>, which
-- is not what someone will guess without a bit more experience.
--
-- NOTE: This won't work in all terminal emulators/tmux/etc. Try your own mapping
-- or just use <C-\><C-n> to exit terminal mode
vim.keymap.set('t', '<Esc><Esc>', '<C-\\><C-n>', { desc = 'Exit terminal mode' })
--?

-- Keybinds to make split navigation easier.
--  Use CTRL+<hjkl> to switch between windows
--
--  See `:help wincmd` for a list of all window commands
map_n('<C-h>', '<C-w><C-h>', 'Move focus to the left window')
map_n('<C-l>', '<C-w><C-l>', 'Move focus to the right window')
map_n('<C-j>', '<C-w><C-j>', 'Move focus to the lower window')
map_n('<C-k>', '<C-w><C-k>', 'Move focus to the upper window')

map_n('<leader>sv', '<CMD>vsplit<CR>', 'Vertical split')
map_n('<leader>sh', '<CMD>split<CR>', 'Horizontal split')

-- lsp
map_n('gd', vim.lsp.buf.definition, 'Go to definition')
map_n('gi', vim.lsp.buf.implementation, 'Go to implementation')
map_n('<leader>ld', vim.diagnostic.open_float, 'Show floating diagnostic')
map_n('<leader>lf', vim.lsp.buf.format, 'Format')
map_n('<leader>lr', vim.lsp.buf.rename, 'Rename symbol')
map_n('<leader>lv', '<CMD>vsplit | lua vim.lsp.buf.definition()<CR>',
	'Go to definition in vertical split')
map_n('<leader>lh', '<CMD>split | lua vim.lsp.buf.definition()<CR>',
	'Go to definition in horizontal split')

-- window
map_n('<leader>wh', '<CMD>resize +5<CR>', 'Higher window')
map_n('<leader>ws', '<CMD>resize -5<CR>', 'Shorter window')
map_n('<leader>ww', '<CMD>vertical resize +5<CR>', 'Wider window')
map_n('<leader>wn', '<CMD>vertical resize -5<CR>', 'Narrower window')
map_n('<leader>wf', '<CMD>vertical resize | resize<CR>', 'Full window')
map_n('<leader>we', '<C-w>=', 'Equal window')

map_n('[t', '<CMD>tabprevious<CR>', 'Previous tab')
map_n(']t', '<CMD>tabnext<CR>', 'Next tab')

map_n('<leader>nb', '<CMD>enew<CR>', 'New buffer')
map_n('<leader>nt', '<CMD>tabnew<CR>', 'New tab')
map_n('<leader>nwh', '<CMD>new<CR>', 'New horizontal window')
map_n('<leader>nwv', '<CMD>new<CR>', 'New vertical window')

map_n('<leader>cb', '<CMD>bdelete<CR>', 'Close current buffer')
map_n('<leader>ct', '<CMD>tabclose<CR>', 'Close current tab')
map_n('<leader>cw', '<CMD>close<CR>', 'Close current window')
